package Vista;

import java.awt.BorderLayout;

import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Font;
import javax.swing.border.LineBorder;
import javax.swing.JTextField;
import java.awt.Dimension;
import javax.swing.JComboBox;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.JTable;

import Controlador.Controlador;
import Modelo.InterfazBD;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Vector;

public class Panel_MCliente_Tienda extends JPanel {

	private Controlador control;
	private InterfazBD interfazBD;
	
	private JPanel panelAzul;
	private JPanel panelVerdeSup;
	private JLabel labelStock;
	private JPanel panelAzulSup;
	private JLabel labelCarrito;
	private JLabel labelFlecha;
	private JPanel panelVerdeCentro;
	private JTable tablaStock;
	private JComboBox comboProductos;
	private JButton botonMenosCant;
	private JTextField textoCantStock;
	private JButton botonMasCant;
	private JButton botonAnadir;
	private JPanel panelAzulCentro;
	private JButton botonAnularProd;
	private JButton botonAnularPed;
	private JTable tablaCarrito;
	private JButton botonComprar;
	private JPanel panelDetalles;
	private JLabel labelTotalCant;
	private JLabel labelTotalEur;
	private JTextField campoTotalCant;
	private JTextField campoTotalEur;
	/**
	 * Create the panel.
	 */
	public Panel_MCliente_Tienda(Controlador control) {
		this.control = control;
		
		this.setLayout(new MigLayout("", "[30.00][400.00,grow][100.00][400.00,grow][30.00]", "[10.00][303.00,grow][10.00]"));
		
		JPanel panelVerde = new JPanel();
		panelVerde.setBorder(new LineBorder(new Color(46, 139, 87), 3));
		panelVerde.setBackground(new Color(204, 255, 204));
		this.add(panelVerde, "cell 1 1,grow");
		
		labelFlecha = new JLabel(">");
		labelFlecha.setFont(new Font("Tahoma", Font.BOLD, 70));
		labelFlecha.setHorizontalAlignment(SwingConstants.CENTER);
		this.add(labelFlecha, "cell 2 1,alignx center,aligny center");
		
		panelAzul = new JPanel();
		panelAzul.setBorder(new LineBorder(new Color(70, 130, 180), 3));
		panelAzul.setBackground(new Color(176, 196, 222));
		this.add(panelAzul, "cell 3 1,grow");
		panelAzul.setLayout(new BorderLayout(0, 0));
		
		panelAzulSup = new JPanel();
		panelAzulSup.setBackground(new Color(70, 130, 180));
		panelAzul.add(panelAzulSup, BorderLayout.NORTH);
		
		labelCarrito = new JLabel("Carrito");
		labelCarrito.setForeground(new Color(255, 255, 255));
		labelCarrito.setBackground(new Color(255, 255, 255));
		labelCarrito.setFont(new Font("Dialog", Font.BOLD, 30));
		panelAzulSup.add(labelCarrito);
		
		panelAzulCentro = new JPanel();
		panelAzulCentro.setBackground(new Color(176, 196, 222));
		panelAzul.add(panelAzulCentro, BorderLayout.CENTER);
		panelAzulCentro.setLayout(new MigLayout("", "[grow][grow]", "[][10.00][grow][10.00][40.00]"));
		
		botonAnularProd = new JButton("Anular producto");
		botonAnularProd.setFont(new Font("Tahoma", Font.PLAIN, 20));
		botonAnularProd.setPreferredSize(new Dimension(109, 40));
		panelAzulCentro.add(botonAnularProd, "cell 0 0,alignx left,aligny center");
		
		botonAnularPed = new JButton("Anular pedido");
		botonAnularPed.setPreferredSize(new Dimension(99, 40));
		botonAnularPed.setFont(new Font("Tahoma", Font.PLAIN, 20));
		panelAzulCentro.add(botonAnularPed, "cell 1 0,alignx right,aligny center");
		
		tablaCarrito = new JTable();
		panelAzulCentro.add(tablaCarrito, "cell 0 2 2 1,grow");
		
		panelDetalles = new JPanel();
		panelDetalles.setBackground(new Color(70, 130, 180));
		panelAzulCentro.add(panelDetalles, "cell 0 4,grow");
		panelDetalles.setLayout(new GridLayout(2, 0, 0, 2));
		
		labelTotalCant = new JLabel("Total cant.");
		labelTotalCant.setForeground(Color.WHITE);
		labelTotalCant.setHorizontalAlignment(SwingConstants.CENTER);
		panelDetalles.add(labelTotalCant);
		
		labelTotalEur = new JLabel("Total €uros");
		labelTotalEur.setForeground(Color.WHITE);
		labelTotalEur.setHorizontalAlignment(SwingConstants.CENTER);
		panelDetalles.add(labelTotalEur);
		
		campoTotalCant = new JTextField();
		campoTotalCant.setBorder(new LineBorder(new Color(70, 130, 180)));
		campoTotalCant.setHorizontalAlignment(SwingConstants.CENTER);
		campoTotalCant.setText("0");
		panelDetalles.add(campoTotalCant);
		campoTotalCant.setColumns(10);
		
		campoTotalEur = new JTextField();
		campoTotalEur.setBorder(new LineBorder(new Color(70, 130, 180)));
		campoTotalEur.setFont(new Font("Dialog", Font.BOLD, 12));
		campoTotalEur.setHorizontalAlignment(SwingConstants.CENTER);
		campoTotalEur.setText("0");
		panelDetalles.add(campoTotalEur);
		campoTotalEur.setColumns(10);
		
		botonComprar = new JButton("Comprar");
		botonComprar.setPreferredSize(new Dimension(99, 40));
		botonComprar.setFont(new Font("Dialog", Font.BOLD, 20));
		panelAzulCentro.add(botonComprar, "cell 1 4,alignx right,aligny center");
		
		panelVerde.setLayout(new BorderLayout(2,2));
		
		panelVerdeSup = new JPanel();
		panelVerdeSup.setBackground(new Color(46, 139, 87));
		panelVerde.add(panelVerdeSup, BorderLayout.NORTH);
		
		labelStock = new JLabel("Stock");
		labelStock.setFont(new Font("Dialog", Font.BOLD, 30));
		labelStock.setForeground(new Color(255, 255, 255));
		panelVerdeSup.add(labelStock);
		
		panelVerdeCentro = new JPanel();
		panelVerdeCentro.setBackground(new Color(204, 255, 204));
		panelVerde.add(panelVerdeCentro, BorderLayout.CENTER);
		panelVerdeCentro.setLayout(new MigLayout("", "[grow][grow]", "[][10.00][grow][10.00][40.00]"));
		
		comboProductos = new JComboBox();
		cargarComboPoductos();
		comboProductos.setPreferredSize(new Dimension(28, 40));
		comboProductos.setFont(new Font("Dialog", Font.PLAIN, 26));
		panelVerdeCentro.add(comboProductos, "cell 0 0 2 1,growx,aligny center");
		
		tablaStock = new JTable();
		panelVerdeCentro.add(tablaStock, "cell 0 2 2 1,grow");
		
		botonMenosCant = new JButton("<");
		botonMenosCant.setFont(new Font("Tahoma", Font.BOLD, 16));
		botonMenosCant.setPreferredSize(new Dimension(41, 40));
		panelVerdeCentro.add(botonMenosCant, "flowx,cell 0 4,alignx left,aligny center");
		
		textoCantStock = new JTextField("1");
		textoCantStock.setMaximumSize(new Dimension(70, 2147483647));
		textoCantStock.setHorizontalAlignment(SwingConstants.CENTER);
		textoCantStock.setFont(new Font("Dialog", Font.PLAIN, 20));
		textoCantStock.setPreferredSize(new Dimension(30, 40));
		panelVerdeCentro.add(textoCantStock, "cell 0 4,alignx left,aligny center");
		textoCantStock.setColumns(10);
		
		botonMasCant = new JButton(">");
		botonMasCant.setFont(new Font("Tahoma", Font.BOLD, 16));
		botonMasCant.setPreferredSize(new Dimension(41, 40));
		panelVerdeCentro.add(botonMasCant, "cell 0 4,alignx left,aligny center");
		
		botonAnadir = new JButton("Añadir");
		botonAnadir.setFont(new Font("Tahoma", Font.PLAIN, 20));
		botonAnadir.setPreferredSize(new Dimension(63, 40));
		panelVerdeCentro.add(botonAnadir, "cell 1 4,alignx right,aligny center");
		
		// Llamada a los oyentes
		comboProductos.addItemListener(new oyenteComboProductos());
        botonMasCant.addActionListener(new oyenteBotonMas());
        botonMenosCant.addActionListener(new oyenteBotonMenos());

		
	}
	
	// METODOS
	public String cabeceraCombo(String tipo) {
		return "Seleccione " + tipo + "...";
	}

	public void cargarComboPoductos() {
		Vector v = control.listadoProductos();
		comboProductos.addItem(cabeceraCombo("producto"));
		for (int i = 0; i < v.size(); i++) {
			comboProductos.addItem((String) v.get(i));
		}
	}
	
	// OYENTES
	class oyenteComboProductos implements ItemListener {
        public void itemStateChanged(ItemEvent ie) {
            if (ie.getStateChange() == ItemEvent.DESELECTED) {
                System.out.println(comboProductos.getSelectedItem().toString());
                if (!comboProductos.getSelectedItem().toString().equalsIgnoreCase(cabeceraCombo("producto"))) {
                    comboProductos.removeAllItems();
                
                    comboProductos.updateUI();
                }
            }
        }
    }
    
    class oyenteBotonMas implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            int cantidad = Integer.parseInt(textoCantStock.getText());
            textoCantStock.setText(String.valueOf(cantidad+1));
        }
    }
    
    class oyenteBotonMenos implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            int cantidad = Integer.parseInt(textoCantStock.getText());
            if (Integer.parseInt(textoCantStock.getText())>1)
                textoCantStock.setText(String.valueOf(cantidad-1));
        }
    }

	
}
